package StudyCode.summary.BufferAndChannel;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.EnumSet;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class RandomAccessFiles {
	public static void main(String[] args) {
		Path path = Paths.get("E:\\Copy", "1.txt");
		try (SeekableByteChannel seekableByteChannel = Files.newByteChannel(path,
				EnumSet.of(StandardOpenOption.READ))) {

			ByteBuffer buffer = ByteBuffer.allocate(12);
			String encoding = System.getProperty("file.encoding");
			buffer.clear();

			while (seekableByteChannel.read(buffer) > 0) {
				buffer.flip();
				System.out.print(Charset.forName(encoding).decode(buffer));
				buffer.clear();
			}
		} catch (IOException ex) {
			System.err.println(ex);
		}
	}
}
